<template>
  <div class="app-container-item">
    <div class="form-main" style="height: calc(100vh - 240px);">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="字典类型">-->
          <!--<el-input v-model="form.dictType" :disabled="true" />-->
        <!--</el-form-item>-->
        <el-form-item label="分类代码" prop="dictValue">
          <el-input v-model="form.dictValue" disabled/>
        </el-form-item>
        <el-form-item label="分类名称" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
        </el-form-item>
        <el-form-item label="回显颜色">
          <el-color-picker
            v-model="form.listClass"
            :show-alpha="false"
            color-format="hex"
            size="mini"
            :predefine="predefineColors">
          </el-color-picker>
        </el-form-item>
        <el-form-item label="显示排序" prop="dictSort">
          <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
        </el-form-item>
        <!--<el-form-item label="回显样式" prop="listClass">-->
          <!--<el-select v-model="form.listClass">-->
            <!--<el-option-->
              <!--v-for="item in listClassOptions"-->
              <!--:key="item.value"-->
              <!--:label="item.label"-->
              <!--:value="item.value"-->
            <!--&gt;</el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div class="dialog-footer" style="text-align: center;">
      <el-button type="primary" plain @click="submitForm">保 存</el-button>
    </div>
  </div>
</template>

<script>
  import { listData,updateData } from "@/api/system/dict/data";

  export default {
    name: "DictForm",
    data() {
      return {
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          dictLabel: [
            { required: true, message: "数据标签不能为空", trigger: "blur" }
          ],
          dictValue: [
            { required: true, message: "数据键值不能为空", trigger: "blur" }
          ],
          dictSort: [
            { required: true, message: "数据顺序不能为空", trigger: "blur" }
          ]
        },
        // 数据标签回显样式
        listClassOptions: [
          {
            value: "default",
            label: "默认"
          },
          {
            value: "primary",
            label: "主要"
          },
          {
            value: "success",
            label: "成功"
          },
          {
            value: "info",
            label: "信息"
          },
          {
            value: "warning",
            label: "警告"
          },
          {
            value: "danger",
            label: "危险"
          }
        ],
        predefineColors:[
          '#ff4500',
          '#ff8c00',
          '#ffd700',
          '#90ee90',
          '#00ced1',
          '#1e90ff',
          '#c71585',
          'rgba(255, 69, 0, 0.68)',
          'rgb(255, 120, 0)',
          'hsv(51, 100, 98)',
          'hsva(120, 40, 94, 0.5)',
          'hsl(181, 100%, 37%)',
          'hsla(209, 100%, 56%, 0.73)',
          '#c7158577'
        ],
        //状态字典
        statusOptions:[]
      };
    },
    watch: {
      currentNode(v) {
        this.initForm()
      }
    },
    created() {
      this.initForm()
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      initForm() {
        this.reset();
        if (this.currentNode.modelType == 'dict') {
          listData({dictType:'report_model_type',dictValue:this.currentNode.id}).then(res => {
            if(res.rows.length > 0){
              this.form = res.rows[0]
            }
          });
        }
      },
      // 表单重置
      reset() {
        this.form = {
          dictCode: undefined,
          dictLabel: undefined,
          dictValue: undefined,
          cssClass: undefined,
          listClass: 'default',
          dictSort: 0,
          status: "0",
          remark: undefined
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateData(this.form).then(response => {
              this.msgSuccess("修改模块类型成功");
              this.open = false;
              this.$emit("ok")
            });
          }
        });
      }
    },
    props: {
      currentNode: {
        type: Object,
        default:()=>{}
      },
    }
  };
</script>
